<?php

/* Page controller class */
class smiley_handler extends _site_handler {
	public $Code;
	public $Smiley;
	public $UserTags = '';
	public $Error;
	public $Message;

	// Render page
	public function __construct($m, $view) {
		$this->InitializeSite($m);

		@list($tmp, $doc_root, $page_name, $smiley_id, $title, $params) = split('/', @$_SERVER['PATH_INFO']);

		$this->Wordlets->AddWordlets('smiley');

		$this->Title = 'Smiley';

		$page = intval(@$_GET['page']);
		if ( !$page ) $page = 1;
		$limit = Monkake::Get('post_limit', true);

		if ( ($smiley_id = intval($smiley_id)) && ($this->Smiley = new Smiley($smiley_id)) ) {
			$this->Smiley->SetPostOffset(($page - 1) * $limit);
			$this->Smiley->SetPostLimit($limit);

			if ( $this->Smiley->Title() ) $this->Title .= ': ' . $this->Smiley->Title();

			if ( $_SERVER['REQUEST_METHOD'] == 'POST' && $this->User && isset($_POST['tags']) && ($tags = split(',', $_POST['tags'])) && count($tags) ) {
				$m->IncludeClass('Inflector');
				$added = array();
				$not_added = array();
				foreach ( $tags as $tag ) {
					if ( ($tag = $this->GetCleanTag($tag)) ) {
						if ( Inflector::isPlural($tag) ) {
							$tag_singular = Inflector::singularize($tag);
							$tag_plural = $tag;
						} else {
							$tag_singular = $tag;
							$tag_plural = Inflector::pluralize($tag);
						}

						$query = "SELECT
										st.id,
										t.id AS tag_id
									FROM
										msnu_sgenre AS t
									LEFT OUTER JOIN
										msnu_smileysgenre AS st ON
											st.sgenre_id=t.id AND
											st.usr_id=" . $this->User->Id . " AND
											st.smiley_id=" . $this->Smiley->Id . "
									WHERE
										t.dname=\"" . mysql_escape_string($tag_singular) . "\" OR
										t.dname=\"" . mysql_escape_string($tag_plural) . "\"
									LIMIT
										1
										";

						$result = Dbi::GetResult($query);

						if ( !$result || !mysql_num_rows($result) ) {
							$query = "INSERT INTO msnu_sgenre SET dname=\"" . mysql_escape_string($tag) . "\"";
							Dbi::GetResult($query);
							$query = "INSERT INTO msnu_smileysgenre SET sgenre_id=" . mysql_insert_id() . ", usr_id=" . $this->User->Id . ", smiley_id=" . $this->Smiley->Id . ", added=NOW(), enabled=1";
							Dbi::GetResult($query);
						} elseif ( $result && ($row = mysql_fetch_assoc($result)) && $row['id'] ) {
							$query = "UPDATE msnu_smileysgenre SET enabled=1 WHERE id=" . $row['id'] . " LIMIT 1";
							Dbi::GetResult($query);
						} elseif ( isset($row) ) {
							$query = "INSERT INTO msnu_smileysgenre SET sgenre_id=" . $row['tag_id'] . ", usr_id=" . $this->User->Id . ", smiley_id=" . $this->Smiley->Id . ", added=NOW(), enabled=1";
							Dbi::GetResult($query);
						}

						$added[] = $tag;
					} else {
						$not_added[] = $tag;
					}
				}

				if ( count($added) ) $this->Message = 'Tags added: ' . htmlentities(implode(', ', $added)) . '.';
				if ( count($not_added) ) $this->Message = 'Tags not added: ' . htmlentities(implode(', ', $not_added)) . '.';
			} elseif ( $_SERVER['REQUEST_METHOD'] == 'POST' && !$this->User ) {
				$this->Error = 'You must log in.';
			} elseif ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
				$this->Error = 'Invalid tags.';
			}
		} else {
			$this->Error = 'Smiley does not exist';
		}

		$this->RenderViewContent($view);
	}
}

?>